package net.login.dao.Impl;

import net.login.dao.UserDao;
import net.login.domain.User;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Administrator on 2017/2/27.
 */
@Repository
public class UserDaoImpl implements UserDao {
    @Resource
    private SessionFactory sessionFactory;

    protected Session getSession(){
        Session session=sessionFactory.getCurrentSession();
        return  session;
    }
    @Override
    public User findByUsernamePassword(String username, String password) {
//        Session session=sessionFactory.openSession();
//        Transaction transaction=session.beginTransaction();
        User user=null;
         user= (User) getSession().createQuery("from User u where u.username= ? and u.password= ?")
                .setParameter(0,username)
                .setParameter(1,password)
                .uniqueResult();
//        transaction.commit();*/
//        session.close();
        return user;
    }

    @Override
    public List<User> findAll() {
        return getSession().createQuery("from User").list();
    }
}
